/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package au.edu.swin.unilearn.submission;

import au.edu.swin.unilearn.assignment.*;
import au.edu.swin.unilearn.students.*;
import au.edu.swin.unilearn.subject.Subject;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author DidierLeung
 */
@Stateless
public class SubmissionSessionBean implements SubmissionSessionRemote
{
    @PersistenceContext
    private EntityManager em;

    public Integer addSubmission(Submission s)
    {
        SubmissionEntity aSubmissionEntity = new SubmissionEntity();
        aSubmissionEntity.setSubmissionEntityData(s);
        Assignment assignment = s.getAssignmentObj();
        AssignmentPK pk = new AssignmentPK(assignment.getAssignment_no(),
                                           (assignment.getSubjectObj()).getSubject_code());
        AssignmentEntity assignmentEntity = em.find(AssignmentEntity.class, pk);
        aSubmissionEntity.setAssignmentEntity(assignmentEntity);
        Student student = s.getAStudent();
        StudentEntity studentEntity = em.find(StudentEntity.class, student.getId());
        studentEntity.addSubmission(aSubmissionEntity);
        aSubmissionEntity.setStudentEntity(studentEntity);        
        em.persist(aSubmissionEntity);
        return aSubmissionEntity.getSubmission_no();
    }

    public void updateSubmission(Submission s)
    {
        SubmissionEntity aSubmissionEntity = em.find(
                                        SubmissionEntity.class, s.getSubmission_no());
        aSubmissionEntity.setSubmission_file_url(s.getSubmission_file_url());
        aSubmissionEntity.setSubmitted_date(s.getSubmitted_date());
        em.persist(aSubmissionEntity);
    }

    public List<Submission> getAllSubmission()
    {
        List<Submission> results = new ArrayList<Submission>();
        Query allSubmissionsQuery = em.createNamedQuery("findAllSubmissions");
        List dbResults = allSubmissionsQuery.getResultList();
        for (int i = 0; i < dbResults.size(); i++)
        {
            SubmissionEntity aSubmissionEntity = (SubmissionEntity) dbResults.get(i);
            results.add(aSubmissionEntity.getSubmissionObject());
        }
        return results;
    }

    public List<Submission> getUnMarkedSubmission()
    {
        List<Submission> results = new ArrayList<Submission>();
        Query allSubmissionsQuery = em.createNamedQuery("findAllUnmarkedSubmission")
                                    .setParameter("status", "Downloaded");
        List dbResults = allSubmissionsQuery.getResultList();
        System.out.println("DBResults Size: " + dbResults.size());
        for (int i = 0; i < dbResults.size(); i++)
        {
            SubmissionEntity aSubmissionEntity = (SubmissionEntity) dbResults.get(i);
            Submission s = aSubmissionEntity.getSubmissionObject();
            Student aStudent = (aSubmissionEntity.getStudentEntity()).getStudentObject();
            s.setAStudent(aStudent);
            results.add(s);
        }
        return results;
    }
    
    public Submission getSubmission(Integer id)
    {
        SubmissionEntity aSubmissionEntity = em.find(SubmissionEntity.class, id);
        return aSubmissionEntity.getSubmissionObject();
    }

    public Integer getNextSubmissionNo()
    {
        Query lastSubmissionNo = em.createNamedQuery("findLastSubmissionNo");
        List dbResults = lastSubmissionNo.getResultList();
        Integer lastNo = (Integer) dbResults.get(0);
        return lastNo + 1;
    }

    public Integer isAlreadySubmitted(String assignmentNo, String subjectCode, String simsId)
    {
        AssignmentPK pk = new AssignmentPK(assignmentNo, subjectCode);
        AssignmentEntity assignmentEntity = em.find(AssignmentEntity.class, pk);
//        System.out.println("ASSIGNMENT NO: " + assignmentEntity.getAssignment_no() + 
//                            " SUBJECT_CODE: " + assignmentEntity.getSubject_code());
        StudentEntity studentEntity = em.find(StudentEntity.class, simsId);
        List<SubmissionEntity> submissionEntityCollection = studentEntity.getSubmissionEntityCollection();
        for (SubmissionEntity submissionEntity : submissionEntityCollection)
        {
            AssignmentEntity other = submissionEntity.getAssignmentEntity();
//            System.out.println("Other ASSIGNMENT NO: " + other.getAssignment_no() + 
//                            " Other SUBJECT_CODE: " + other.getSubject_code());
            if (other.equals(assignmentEntity))
            {
                return submissionEntity.getSubmission_no();
            }
        }
        return null;
    }
    
    public Submission getSubmission(String studentID, String assignmentNo, String subjectCode)
    {
        List<Submission> allSubmissions  = getUnMarkedSubmission();
        for(Submission s : allSubmissions)
        {
            System.out.println("Submission #" + s.getSubmission_no());
            Student aStudent = s.getAStudent();
            System.out.println("Student in Comp: " + studentID);
            if(studentID.equals(aStudent.getId()))
            {
                System.out.println("FOUND SUBMISSION FOR " + studentID);
                Assignment a = s.getAssignmentObj();
                Subject aSubject = a.getSubjectObj();
                if(assignmentNo.equals(a.getAssignment_no()) 
                        && subjectCode.equals(aSubject.getSubject_code()))
                {
                    System.out.println("FOUND SUBMISSION FOR ASS# " + assignmentNo);
                    return s;
                }
            }
        }
        return null;
    }
    
    public void updateSubmission(String studentID, String assignmentNo,
                            String subjectCode, String detailFileURL, double result)
    {
        Submission submissionObj = this.getSubmission(studentID, assignmentNo, subjectCode);
        SubmissionEntity submissionEntity = em.find(SubmissionEntity.class, submissionObj.getSubmission_no());
        submissionEntity.setResults(result);
        submissionEntity.setDetail_file_url(detailFileURL);
        submissionEntity.setSubmission_status("Marked");
        em.persist(submissionEntity);
    }
}
